package com.vcredit.creditcat.service;

import com.vcredit.creditcat.dto.RegionalismDto;
import com.vcredit.creditcat.model.SysNation;
import com.vcredit.creditcat.persistence.base.SysNationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by liaoshengjun on 2017/8/8.
 */
@Service
public class SysNationService {
	@Autowired
	private SysNationMapper mapper;
	public List<SysNation> findAllProvinces(){return mapper.findAllProvinces();}
	public List<SysNation> findAllCities(){return mapper.findAllCities();}
	public List<SysNation> findAllDistricts(){return mapper.findAllDistricts();}
	public SysNation findByCode(@NotNull String code){return mapper.findByCode(code);}

	/**
	 * 分层级获取所有的行政区划
	 * @return
	 */
	public List<RegionalismDto> getAllRegionalisms(){
		List<RegionalismDto> allRegionalisms=new ArrayList<>();
		List<SysNation> allProvinces=mapper.findAllProvinces();
		List<SysNation> allCities=mapper.findAllCities();
		List<SysNation> allDistricts=mapper.findAllDistricts();
		for (SysNation province:allProvinces){
			RegionalismDto regionalismDto=new RegionalismDto();
			regionalismDto.setAreaCode(province.getCode());
			regionalismDto.setAreaName(province.getProvince());
			List<RegionalismDto> cities=new ArrayList<>();
			regionalismDto.setRegionalisms(cities);
			for(SysNation city:allCities){
				if(city.getParentId().equals(province.getId()) || city.getId().equals(province.getId())){
					RegionalismDto regionalismDto1=new RegionalismDto();
					regionalismDto1.setAreaCode(city.getCode());
					regionalismDto1.setAreaName(city.getCity());
					List<RegionalismDto> districts=new ArrayList<>();
					regionalismDto1.setRegionalisms(districts);
					cities.add(regionalismDto1);
					for(SysNation district:allDistricts){
						if(district.getParentId().equals(city.getId())){
							RegionalismDto regionalismDto2=new RegionalismDto();
							regionalismDto2.setAreaCode(district.getCode());
							regionalismDto2.setAreaName(district.getDistrict());
							districts.add(regionalismDto2);
						}
					}
				}
			}
			allRegionalisms.add(regionalismDto);
		}
		return allRegionalisms;
	}
}
